# -*- coding:utf-8 -*-

import psutil
import re
import os
import sendmail
a=[]
c=[]

def process(x):
    p = psutil.pids()
    for i in p:
        pid=psutil.Process(i)
        name = str(pid.name())
        if re.match(x, name):
            a.append('%i' %i)


try:
    process('java')
    if len(a) < 16:
        for g in a:
            pid=psutil.Process(int(g))
            l=pid.cwd()
            if re.match("^.*\-tdx.*$", l):
               c.append('%i' %int(g))
        sendmail.send_mail('maintenance@xinyusoft.com@xinyusoft.com','交易程序故障报警','现有进程数:%i\ntdx进程数:%i' %(len(a),len(c)))
        sendmail.send_mail('zengjianfeng@xinyusoft.com@xinyusoft.com','交易程序故障报警','现有进程数:%i\ntdx进程数:%i' %(len(a),len(c)))
        sendmail.send_mail('gusheng@xinyusoft.com@xinyusoft.com','交易程序故障报警','现有进程数:%i\ntdx进程数:%i' %(len(a),len(c)))
except Exception as e:
   sendmail.send_mail('xitongjiankong@xinyusoft.com','交易程序故障报警','监控程序异常,请处理\nerror%s' %e)